package com.sgb.kjwl.view.ui.activity.home;

import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.capital.view.widget.MToast;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FindByMapActivityBinding;
import com.sgb.kjwl.model.entity.self.TypeChooseEntity;
import com.sgb.kjwl.view.constant.RequestCode;
import com.sgb.kjwl.view.ui.adapter.YoungAdapterOnItemClick;
import com.sgb.kjwl.view.ui.adapter.transport.FindMapTabAdapter;
import com.sgb.kjwl.view.ui.fragment.home.MapFindCarFragment;
import com.sgb.kjwl.view.ui.fragment.home.MapFindGoodsSourceFragment;
import com.sgb.link.entity.AMapAddressEntity;
import com.sgb.link.router.PathUser;
import com.sgb.link.service.MapService;
import com.swgk.core.base.model.entity.RouterKey;

import java.util.ArrayList;
import java.util.List;

import static com.sgb.link.router.PathTransport.FindByMapActivity;

/**
 * Describe:    地图--找车/找货
 * Author:      杨磊
 * Date:        2021/11/23
 */
@Route(path = FindByMapActivity)
public class FindByMapActivity extends AppActivity {
    public static final int REQUEST_CODE_GOODS = 201;
    public static final int REQUEST_CODE_CAR = 202;
    public static final String FRAGMENT_TAG_CAR = "car";
    public static final String FRAGMENT_TAG_GOODS = "goods";

    @Autowired(name = RouterKey.DATA, desc = "装货地址信息")
    public AMapAddressEntity loadingEntity;
    @Autowired(name = RouterKey.DATA1, desc = "卸货地址信息")
    public AMapAddressEntity unloadingEntity;
    @Autowired(name = RouterKey.INT, desc = "标示地图找车还是地图找货")
    public int tabIndex;
    @Autowired(name = RouterKey.INT1, desc = "选中的货物重量位置")
    public int selectWeightPosition;//weightBeans 数据需要与传过来的地方保持一致
    @Autowired(name = RouterKey.INT2, desc = "选中搜索半径")
    public int selectRadiusPosition;
    @Autowired(name = RouterKey.LIST, desc = "选中的货物类型集合")
    public ArrayList<TypeChooseEntity> goodsTypes;
    @Autowired(name = RouterKey.LIST1, desc = "选中的车型集合")
    public ArrayList<TypeChooseEntity> carTypeEntities;
    @Autowired(name = RouterKey.LIST2, desc = "选中的功能集合")
    public ArrayList<TypeChooseEntity> carFunctionEntities;

    @Autowired(name = PathUser.AMapDialog)
    public MapService mapService;

    private FindByMapActivityBinding binding;
    private FindMapTabAdapter tabAdapter;
    private FragmentManager fragmentManager;
    private FragmentTransaction beginTransaction;
    private MapFindGoodsSourceFragment sourceFragment;
    private MapFindCarFragment carFragment;

    @Override
    protected void init() {
        ARouter.getInstance().inject(this);
        binding = DataBindingUtil.setContentView(this, R.layout.find_by_map_activity);
        initTab();
        initContainer();
        initEvent();
    }

    private void initTab() {
        List<String> tabs = new ArrayList<>();
        tabs.add("地图找车");
        tabs.add("地图找货");
        tabs.add("发布管理");
        tabAdapter = new FindMapTabAdapter(this, tabs);
        binding.rvTab.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));
        binding.rvTab.setAdapter(tabAdapter);
        tabAdapter.setMostNumber(1);
    }

    /**
     * 选择地址
     *
     * @param keyword 详细地址
     * @param type    0——表示起点地址    1——表示终点地址
     */
    public void selectAddress(String keyword, int type) {
        if (mapService != null) {
            mapService.launchMap(this, type, keyword);
        }
    }

    private void initContainer() {
        fragmentManager = getSupportFragmentManager();
        beginTransaction = fragmentManager.beginTransaction();
        carFragment = MapFindCarFragment.newInstance(loadingEntity, unloadingEntity, carTypeEntities, carFunctionEntities, selectRadiusPosition);
        sourceFragment = MapFindGoodsSourceFragment.newInstance(loadingEntity, unloadingEntity, goodsTypes, selectWeightPosition, selectRadiusPosition);
        beginTransaction.add(R.id.fragment_container, carFragment, FRAGMENT_TAG_CAR);
        beginTransaction.add(R.id.fragment_container, sourceFragment, FRAGMENT_TAG_GOODS);
        if (tabIndex == 0) {//找车
            beginTransaction.show(carFragment);
            beginTransaction.hide(sourceFragment);
            tabAdapter.setSelectIndex(0);
        } else if (tabIndex == 1) {//找货
            beginTransaction.show(sourceFragment);
            beginTransaction.hide(carFragment);
            tabAdapter.setSelectIndex(1);
        }
        beginTransaction.commit();
    }

    private void initEvent() {
        binding.ivBack.setOnClickListener(v -> finish());
        tabAdapter.setOnItemClick(new YoungAdapterOnItemClick<String>() {
            @Override
            public void onItemClick(String s, int position) {
                tabIndex = position;
                if (position != 2) {
                    tabAdapter.setSelectIndex(position);
                }
                beginTransaction = fragmentManager.beginTransaction();
                switch (position) {
                    case 0:
                        beginTransaction.show(carFragment);
                        beginTransaction.hide(sourceFragment);
                        beginTransaction.commitNow();
                        break;
                    case 1:
                        beginTransaction.show(sourceFragment);
                        beginTransaction.hide(carFragment);
                        beginTransaction.commitNow();
                        break;
                    case 2:
                        MToast.showToast(FindByMapActivity.this, "功能正在开发中");
                        break;
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && requestCode == RequestCode.CODE_MAP && resultCode == RESULT_OK) {
            AMapAddressEntity entity = data.getParcelableExtra(RouterKey.DATA);
            if (entity != null) {
                if (tabIndex == 0) {
                    carFragment.loadingAddressInfo(entity);
                } else if (tabIndex ==1) {
                    sourceFragment.loadingAddressInfo(entity);
                }

            }
        }
    }
}